//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TermStructure.h"
using namespace Cephei::QL;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Math/Interpolations/Extrapolator.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Math::Interpolations;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CTermStructure::CTermStructure (boost::shared_ptr<QuantLib::TermStructure>& childNative, Object^ owner) : CExtrapolator(CTermStructure::typeid)
{
#ifdef HANDLE
	_phTermStructure = NULL;
#endif
	_ppTermStructure = &childNative;
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
}
Cephei::QL::CTermStructure::CTermStructure (QuantLib::TermStructure& childNative, Object^ owner) : CExtrapolator(CTermStructure::typeid)
{
#ifdef HANDLE
	_phTermStructure = NULL;
#endif
	_ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (&childNative);
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
    _TermStructureOwner = owner;
    _ExtrapolatorOwner = owner;
}

Cephei::QL::CTermStructure::CTermStructure (CTermStructure^ copy) : CExtrapolator(CTermStructure::typeid)
{
#ifdef HANDLE
	_phTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (copy->GetShared());
        _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
    }
}
Cephei::QL::CTermStructure::CTermStructure (PLATFORM::Type^ t) : CExtrapolator(CTermStructure::typeid)
{
#ifdef HANDLE
	_phTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CTermStructure::CTermStructure (QuantLib::Handle<QuantLib::TermStructure>& childNative, Object^ owner)  : CExtrapolator(CTermStructure::typeid)
{
	_phTermStructure = &childNative;
	_ppTermStructure = &static_cast<boost::shared_ptr<QuantLib::TermStructure>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
    _TermStructureOwner = owner;
}
Cephei::QL::CTermStructure::CTermStructure (QuantLib::Handle<QuantLib::TermStructure> childNative)  : CExtrapolator(CTermStructure::typeid)
{
	_phTermStructure = &childNative;
	_ppTermStructure = &static_cast<boost::shared_ptr<QuantLib::TermStructure>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::CTermStructure::CTermStructure (QuantLib::TermStructure childNative)  : CExtrapolator(CTermStructure::typeid)
{
#ifdef HANDLE
	_phTermStructure = NULL;
#endif
	_ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (new QuantLib::TermStructure (childNative));
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppTermStructure));
}
#endif

Cephei::QL::CTermStructure::~CTermStructure ()
{
    if (_ppTermStructure != NULL)
    {
	    delete _ppTermStructure;
        _ppTermStructure = NULL;
    }
}
Cephei::QL::CTermStructure::!CTermStructure ()
{
    if (_ppTermStructure != NULL)
    {
	    delete _ppTermStructure;
    }
}
QuantLib::TermStructure& Cephei::QL::CTermStructure::GetReference ()
{
    if (_ppTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppTermStructure;
}
boost::shared_ptr<QuantLib::TermStructure>& Cephei::QL::CTermStructure::GetShared ()
{
    if (_ppTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppTermStructure;
}
QuantLib::TermStructure* Cephei::QL::CTermStructure::GetPointer ()
{
    if (_ppTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TermStructure>& Cephei::QL::CTermStructure::GetHandle ()
{
	if (_phTermStructure == NULL)
	{
		_phTermStructure = new Handle<QuantLib::TermStructure> (*_ppTermStructure);
	}
	return *_phTermStructure;
}
#endif
bool Cephei::QL::CTermStructure::HasNative () 
{
	return (_ppTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::CTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::CTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::CTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTermStructure::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTermStructure)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::CTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::CTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTermStructure::TimeFromReference (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTermStructure)->timeFromReference ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ITermStructure^ Cephei::QL::CTermStructure::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppTermStructure)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

